#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <mutex>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include "cppjieba/Jieba.hpp"

namespace ns_util
{
    namespace FileUtil
    {
        bool ReadFile(const std::string &file, std::string *output)
        {
            std::ifstream in(file, std::ios::in);
            if (!in.is_open())
            {
                std::cerr << "open file:" << file << "error." << std::endl;
                return false;
            }

            std::string line;
            while (std::getline(in, line)) // getline 读取文件时会返回引用然后被强转为bool值，文件读取到结尾会直接返回false
            {
                (*output) += line;
            }

            in.close();

            return true;
        }
    }

    class StringUtil
    {
    public:
        static void CutString(const std::string line, std::vector<std::string> *result, const std::string &sep)
        {
            boost::split(*result, line, boost::is_any_of(sep), boost::token_compress_on);
        }
    };

    const char *const DICT_PATH = "./dict/jieba.dict.utf8";
    const char *const HMM_PATH = "./dict/hmm_model.utf8";
    const char *const USER_DICT_PATH = "./dict/user.dict.utf8";
    const char *const IDF_PATH = "./dict/idf.utf8";
    const char *const STOP_WORD_PATH = "./dict/stop_words.utf8";

    class JiebaUtil
    {
    private:
        cppjieba::Jieba jieba;
        std::unordered_set<std::string> stop_words;
        static JiebaUtil* instance;
    private:
        JiebaUtil() : jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH){}
        ~JiebaUtil(){}
        JiebaUtil(const JiebaUtil&) = delete;
        JiebaUtil& operator=(const JiebaUtil&) = delete;

        static JiebaUtil* GetInstance()
        {
            static std::mutex mutex;
            if(nullptr == instance)
            {
                mutex.lock();
                if(nullptr == instance)
                {
                    instance = new JiebaUtil();
                    instance->JiebaInit();
                }
                mutex.unlock();
            }
            return instance;
        }

        void JiebaInit()
        {
            std::ifstream in(STOP_WORD_PATH, std::ios::in);
            if(!in.is_open())
            {
                std::cout << "STOP_WORD_PATH open fail!" << std::endl;
            }

            std::string line;
            while(std::getline(in, line))
            {
                stop_words.insert(line);
            }

            in.close();
        }

        void CutStringHelper(const std::string &src, std::vector<std::string> *out)
        {
            jieba.CutForSearch(src, *out);
            for(auto iter = out->begin(); iter < out->end(); )
            {
                if(stop_words.find(*iter) != stop_words.end())
                {
                    out->erase(iter);
                }
                else
                {
                    iter++;
                }
            }
        }

    public:
        static void CutString(const std::string &src, std::vector<std::string> *out)
        {
            GetInstance()->CutStringHelper(src, out);
        }
    };
    JiebaUtil* JiebaUtil::instance = nullptr;
}
